import type { Request, Response } from 'express';

import { z } from 'zod';

import { RepoModel } from '../../../models/repo';
import Resp from '../../../utils/response';

// 参数校验规则
export const validator = z.object({
  ID: z
    .string({
      invalid_type_error: '仓库ID必须是字符串',
      required_error: '仓库ID不能为空',
    })
    .min(1, '仓库ID不能为空'),
});

// 删除仓库API
export const api = async (req: Request, res: Response): Promise<Response> => {
  try {
    const { ID } = req.params as z.infer<typeof validator>;

    // 检查仓库是否存在并删除
    const result = await RepoModel.findByIdAndDelete(ID);
    if (!result) {
      return Resp.error(res, 404, '仓库不存在');
    }

    // 返回成功响应
    return Resp.success(res, null, '仓库删除成功');
  } catch (error) {
    console.error(error);
    return Resp.catch(res, error);
  }
};
